﻿using TrueSync;

[System.Serializable]
public class StandDefenseState : StateBase
{
    LogicMoveComponent mMoveComponent;
    LogicInputComponent mInputComponent;

    public override StateEnum StateName
    {
        get
        {
            return StateEnum.StandDefense; 
        }
    }

    public override void Enter(LogicEntity lEntity, StateBase lastState, object[] stateParams)
    {
        base.Enter(lEntity, lastState, stateParams);
        mLEntity.GetLogicComponent<LogicMoveComponent>().MoveSpeed = TSVector2.zero;
        mMoveComponent = lEntity.GetLogicComponent<LogicMoveComponent>();
        mInputComponent = lEntity.GetLogicComponent<LogicInputComponent>();
    }

    public override void Excute(FP deltaTime)
    {
        LogicStateMachineComponent stateMachineComponent = mLEntity.GetLogicComponent<LogicStateMachineComponent>();
        if (statusComponent.HP <= 0)
        {
            stateMachineComponent.ChangeState(StateEnum.Dead);
            return;
        }

        if (stateMachineComponent.IsTouchingRivalsGuardAlarmRect == false)
        {
            stateMachineComponent.ChangeState(StateEnum.WalkBackward);
        }

        mMoveComponent.FaceOther();
    }

    public override void Exit()
    {
        mMoveComponent = null;
        mInputComponent = null;
        base.Exit();
    }

    public override void OnInputDirection(int direction)
    {
        switch(direction)
        {
            case 5:
                mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.Stand);
                break;
            case 1:
            case 2:
            case 3:
                mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.Crouch);
                break;
            case 6:
                mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.WalkForward);
                break;
            
            case 7:
                mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.JumpBackward);
                break;
            case 8:
                mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.Jump);
                break;
            case 9:
                mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeState(StateEnum.JumpForward);
                break;
        }
    }

    public override void OnStimulate(Stimulate stimulate)
    {
        LogicStateMachineComponent stateMachineComponent = mLEntity.GetLogicComponent<LogicStateMachineComponent>();
        switch (stimulate.StimulateType)
        {
            //这里把防御住以后其他的判定都转成击退判定,达到一致的效果
            case StimulateTypeEnum.HitBackStimulate:
                HitBackStimulate hitbackStimulate = (HitBackStimulate)stimulate;
                if (!stimulate.HasDownStimulate)
                {
                    stateMachineComponent.ChangeState(StateEnum.StandDefenseHit, hitbackStimulate.XMoveLength, hitbackStimulate.XMoveSpeed);
                }
                else
                {
                    stateMachineComponent.ChangeState(StateEnum.StandHit, hitbackStimulate.XMoveLength, hitbackStimulate.XMoveSpeed);
                }
                break;
            case StimulateTypeEnum.FalldownStimulate:
                stateMachineComponent.ChangeState(StateEnum.KnockdownHit);
                break;
            case StimulateTypeEnum.HitFloatStimulate:
                HitFloatStimulate hitfloatStimulate = (HitFloatStimulate)stimulate;
                stateMachineComponent.ChangeState(StateEnum.FloatHit, hitfloatStimulate.XMoveLength, hitfloatStimulate.XMoveSpeed, hitfloatStimulate.YHeight);
                break;
            case StimulateTypeEnum.HitFlyStimulate:
                HitFlyStimulate hitflyStimulate = (HitFlyStimulate)stimulate;
                stateMachineComponent.ChangeState(StateEnum.HitFly, hitflyStimulate.XMoveLength, hitflyStimulate.XMoveSpeed);
                break;
            case StimulateTypeEnum.ThrowStimulate:
                stateMachineComponent.ChangeState(StateEnum.ThrowHit);
                break;
        }
    }
}
